define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'clothmaking_testing/index' + location.search,
                    add_url: 'clothmaking_testing/add',
                    edit_url: 'clothmaking_testing/edit',
                    del_url: 'clothmaking_testing/del',
                    multi_url: 'clothmaking_testing/multi',
                    import_url: 'clothmaking_testing/import',
                    table: 'clothmaking_testing',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},                       
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'planid', title: __('Planid')},
                        {field: 'density', title: __('Density'), operate:'BETWEEN'},
                        {field: 'thickness', title: __('Thickness'), operate:'BETWEEN'},
                        {field: 'cld', title: __('Cld'), operate:'BETWEEN'},
                        {field: 'astm23', title: __('Astm23'), operate:'BETWEEN'},
                        {field: 'astm70', title: __('Astm70'), operate:'BETWEEN'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,

                            buttons: [
                                {
                                    name: 'view',
                                    title: '检测单',
                                    classname: 'btn btn-warning btn-xs btn-dialog btn-viewsub',
                                    icon: 'fa fa-table',

                                }
                            ]
                            
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('click', '.btn-viewsub', function(e) {
                e.stopPropagation();

                var options = table.bootstrapTable('getOptions');

                var index = $(this).closest('tr').data('index');

                var data = table.bootstrapTable('getData');
             
                var id = data[index][options.pk]

                Fast.api.open('clothmaking_testing/view?id='+ id , '检测单');

            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
